package com.base.data.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.data.dao.ProvenceDao;
import com.base.data.entity.Provence;
import com.mysteel.page.Page;
import com.mysteel.util.ObjectUtils;

@Service("provenceService")
public class ProvenceServiceImpl implements ProvenceService
{
	@Autowired
	private ProvenceDao provenceDao;
	
	@Override
	public Page<Provence> page(Map<String, Object> params, int currentPageNum,
			int maxPageRowCount)
	{
		Page<Provence> page = null;
		
		if (ObjectUtils.isNull (params))
		{
			params = new HashMap<String, Object> (2);
		}
		
		int count = provenceDao.count ();
		
		if (count > 0)
		{
			page = new Page<Provence> ();
			page.setTotalRowCount (count);
			page.setCurrentPageNum (currentPageNum);
			page.setMaxPageRowCount (maxPageRowCount);
			page.setURL (params);
			
			params.put ("startPageRowCount" ,page.getStartPageRowCount ());
			params.put ("maxPageRowCount" ,page.getMaxPageRowCount ());
			
			List<String> ProvenceIds = provenceDao.page (params);
			
			if (ObjectUtils.notEmpty (ProvenceIds))
			{
				List<Provence> Provences = new ArrayList<Provence> (
						ProvenceIds.size ());
				
				for( String ProvenceId : ProvenceIds)
				{
					Provences.add (provenceDao.getById (ProvenceId));
				}
				
				page.setDatas (Provences);
			}
		}
		
		return page;
	}
	
	@Override
	public void save(Provence provence)
	{
		
		provenceDao.add (provence);
		
	}
	
	@Override
	public Provence getProvenceById(String id)
	{
		
		return provenceDao.getById (id);
	}
	
	@Override
	public Provence getProvenceByName(String name)
	{
		
		return provenceDao.getByName (name);
	}
	
	@Override
	public void deleteById(String id)
	{
		
		provenceDao.delete (id);
		
	}
	
	@Override
	public void udpateProvence(Provence provence)
	{
		
		Provence p = provenceDao.getById (provence.getId ());
		if (ObjectUtils.notNull (p))
		{
			
			p.setName (provence.getName ());
			p.setDescription (provence.getDescription ());
			p.setareaId (provence.getareaId ());
			
			provenceDao.update (p);
		}
		
	}

	@Override
	public List<Provence> queryProvence()
	{
		return provenceDao.query ();
	}
	
}
